package com.chaoyong.higo.fragment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.chaoyong.higo.R;
import com.chaoyong.higo.activity.ProductDetailActivity;
import com.chaoyong.higo.adapter.ZuixinjiexiaoAdapter;
import com.chaoyong.higo.base.BaseFragment;
import com.chaoyong.higo.bean.WiningGoodsBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.view.XListView;
import com.chaoyong.higo.view.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class FragShaiDan extends BaseFragment implements IXListViewListener, OnItemClickListener {

	private XListView xlv;
	private List<WiningGoodsBean.DataEntity.ListEntity> datas;
	private ZuixinjiexiaoAdapter adapter;
	private Activity act;
	private WiningGoodsBean bean;
	private int page = 5;

	private TextView title_center_text;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.frag_shaidan, null, false);
		return mView;
	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub
		act = getActivity();

		title_center_text = (TextView) mView.findViewById(R.id.title_center_text);
		title_center_text.setText("晒单");

		xlv = (XListView) mView.findViewById(R.id.shaidan_xlv);
		adapter = new ZuixinjiexiaoAdapter(act, null);
		xlv.setAdapter(adapter);

		xlv.setPullLoadEnable(true);
		xlv.setPullRefreshEnable(true);
		xlv.setXListViewListener(this);
		xlv.setOnItemClickListener(this);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if(datas != null){
			datas.clear();
		}
		if(datas == null){
			//httpQueryNewAnnounced();
		}else{
			//adapter.changeDatas(datas);
		}
		
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// httpQueryNewAnnounced();
	}

	private void httpQueryNewAnnounced() {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject object = new JSONObject();

			object.putOpt("model", "GoodsWinning");
			object.putOpt("function", "getWinningAll");
			object.putOpt("code", "5");
			object.putOpt("app", 1);

			param.setBodyEntity(new StringEntity(object.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Public_appPort, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				Gson gson = new Gson();
				datas = new ArrayList<WiningGoodsBean.DataEntity.ListEntity>();
				bean = gson.fromJson(result, WiningGoodsBean.class);
				if (bean.getStatus().equals("1")) {
					datas = bean.getData().getList();
					// showToast("网络查询完毕"+ entities.size());
					if (!EmptyUtil.isEmpty(datas)) {
						adapter.changeDatas(datas);
					}
				}
			}
		});

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		//datas.clear();
		//httpQueryNewAnnounced();
		xlv.stopRefresh();
		Toast.makeText(act, "已经刷新", 0).show();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		loadMore();
		xlv.stopLoadMore();
	}

	private void loadMore() {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject object = new JSONObject();

			object.putOpt("model", "GoodsWinning");
			object.putOpt("function", "getWinningAll");
			object.putOpt("code", "5," + page + ",5");
			object.putOpt("app", 1);

			param.setBodyEntity(new StringEntity(object.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Public_appPort, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if (jso.getInt("status") == 0) {
						Toast.makeText(act, "没有更多数据了", 0).show();
						xlv.stopLoadMore();
					} else {
						Gson gson = new Gson();
						bean = gson.fromJson(arg0.result, WiningGoodsBean.class);
						if (bean.getStatus().equals("1")) {
							datas.addAll(bean.getData().getList());
							// showToast("网络查询完毕"+ entities.size());
							if (!EmptyUtil.isEmpty(datas)) {
								adapter.changeDatas(datas);
								page += 5;
							}
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		switch (parent.getId()) {
		case R.id.shaidan_xlv:// 主布局的item点击事件
			Intent intent = new Intent(mContext, ProductDetailActivity.class);
			intent.putExtra("goods_id", datas.get(position - 1).getGoods_id());
			intent.putExtra("expect", datas.get(position - 1).getExpect());
			intent.putExtra("goods_img", datas.get(position - 1).getCover_img().get(0));
			intent.putExtra("head_img", datas.get(position - 1).getUid_portrait());
			intent.putExtra("price", datas.get(position - 1).getGoods_price());
			intent.putExtra("goodsInfo", datas.get(position - 1).getGoods_info());
			startActivity(intent);
			break;

		default:
			break;
		}
	}

}
